<?php
// +----------------------------------------------------------------------
// | 中联致远服务端框架
// +----------------------------------------------------------------------
// | Copyright (c) 2018 http://www.zhongliansoft.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: tony <tanyu@zhongliansoft.com>
// +----------------------------------------------------------------------

namespace app\common\model;
use think\Validate;
use think\model\concern\SoftDelete;
use think\facade\Log;

class SupplyBookInfo extends Common
{
    protected $rule = [
        'plan_book_num'=> 'require|number',
    ];

    protected $msg = [
        'plan_book_num.require' => '请输入教材数量',
        'plan_book_num.number' => '教材数量必须是数字',
    ];
  
    /**
     * @param $post
     *
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function tableData($post)
    {
        if(isset($post['limit'])){
            $limit = $post['limit'];
        }else{
            $limit = config('paginate.list_rows');
        }

        $tableWhere = $this->tableWhere($post);
        $list = $this->alias('sbi')
                ->field("sbi.id, sbi.plan_price, sbi.book_disc, sbi.plan_book_num, book.id as book_id, book.book_name, book.book_isbn") 
                ->join('zl_book_info book', 'book.id = sbi.plan_book_id')
                ->where($tableWhere['where'])->order($tableWhere['order'])->paginate($limit);
        $data = $this->tableFormat($list->getCollection());         //返回的数据格式化，并渲染成table所需要的最终的显示数据类型

        $re['code'] = 0;
        $re['msg'] = '';
        $re['count'] = $list->total();
        $re['data'] = $data;

        return $re;
    }

    //where搜索条件
    protected function tableWhere($post)
    {
        $where = [];
        if(isset($post['operid']) && $post['operid'] != ""){
            $where[] = ['sbi.plan_operid', 'eq', $post['operid']];
        }

        $result['where'] = $where;
        $result['field'] = "*";
        $result['order'] = ['id ASC'];
        return $result;
    }


    /**
     * @param $list
     *
     * @return mixed
     */
    protected function tableFormat($list)
    {
        return $list;
    }
  
    /**
     * 增加教材
     * @param $data
     * @return array
     */
    public function add($data)
    {
        $return = [
            'status' => false,
            'msg'    => '增加失败',
            'data'   => ''
        ];

        // 特殊情况处理
        //校验数据
        $validate = new Validate($this->rule, $this->msg);
        if (!$validate->check($data)) {
            $return['msg'] = $validate->getError();
            return $return;
        } 
        
        // 判断是否有重复数据
        $plan_book_info = $this->where(['plan_operid' =>$data['plan_operid'], 'plan_book_id' => $data['plan_book_id']])->find();
        if($plan_book_info) {
            $return['msg'] = '已存在此教材！';
            return $return;
        }

        // if($data['plan_price'] <= 0) {
        //     $return['msg'] = '请输入合法的单价';
        //     return $return;
        // }

        if($data['plan_book_num'] <= 0) {
            $return['msg'] = '请输入合法的数量';
            return $return;
        }

        $this->save($data);

        $return['status'] = true;
        $return['msg']    = '增加成功'; 

        \think\facade\Request::token('__ZlServer_Token__', 'sha1');
        return $return;
    }
}